home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / QuickTime VR / MacOS / QuickDraw™ 3D 1.0.6F4 SDK / Development / 3DMF parser / 1.0 version / MF3DMac / MFMEMORY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-07  |  4.7 KB  |  213 lines  |  [TEXT/MPS ]

  1. /*==============================================================================
  2.  *
  3.  *    File:        MFMEMORY.C
  4.  *
  5.  *    Function:    Memory allocation routines
  6.  *
  7.  *    Version:    Metafile:    Version 1.0 3DMF files
  8.  *                Package:    Release #2 of this code
  9.  *
  10.  *    Author(s):    Rick Wong (RWW), Duet Development Corp.
  11.  *                John Kelly (JRK), Duet Development Corp.
  12.  *
  13.  *    Copyright:    (c) 1995 by Apple Computer, Inc., all rights reserved.
  14.  *
  15.  *    Change History (most recent first):
  16.  *        FB8_JRK    Segmentation
  17.  *        FB7_JRK    Deal with MetroWerks allocation "Bug"
  18.  *        FB6_JRK Accept real MF3D_Uns32 and check for size_t overflow
  19.  *        FAK_JRK    Added error debugging when DEBUG > 0
  20.  *        F7K_PAM bug fixing for null pointers and sizes of zero
  21.  *        Fabio    Changed file name to 8 characters
  22.  *        F3L_RWW    File created.
  23.  *==============================================================================
  24.  */
  25. #define MF3D_MEMORY_C
  26. #include "MFMEMORY.H"
  27. #undef MF3D_MEMORY_C
  28.  
  29. #if defined(DEBUG) && DEBUG > 0
  30. #include <stdio.h>        /* printf for debugging */
  31. #endif
  32. #include <limits.h>        /* CHAR_BIT */
  33.  
  34. #include "MFASSERT.H"
  35.  
  36. #if defined(applec) || defined(__MWERKS__) || defined(THINK_C)
  37. #pragma segment __MF3D__
  38. #endif
  39.  
  40.  
  41. /* FB7_JRK
  42.  * MetroWerks doesn't return pointers > 32K back to Mac OS we we call free()
  43.  * so we have use NewPtr and DisposePtr directory to allocate memory.
  44.  */
  45.  
  46. #if defined(__MWERKS__) && (defined(__MC68K__) || defined(__POWERPC__))
  47. # define kUseNewPtrDirectly    1
  48. #else
  49. # define kUseNewPtrDirectly    0
  50. #endif
  51.  
  52.  
  53. #if kUseNewPtrDirectly
  54. #include <Memory.h>
  55. #endif
  56.  
  57.  
  58. /* FB6_JRK */
  59. /* Based on size_t, calculate max positive value for allocation. */
  60. #if !kUseNewPtrDirectly
  61. static const unsigned long gMaxAllocSize =
  62.     ((size_t)-1) > 0 ?
  63.         (size_t)-1 :                                    /* size_t unsigned    */
  64.         (1UL << (sizeof(size_t) * CHAR_BIT - 1)) - 1;    /* size_t signed    */
  65. #else
  66. static const unsigned long gMaxAllocSize =
  67.     ((Size)-1) > 0 ?
  68.         (Size)-1 :                                        /* Size unsigned    */
  69.         (1UL << (sizeof(Size) * CHAR_BIT - 1)) - 1;        /* Size signed    */
  70. #endif /* kUseNewPtrDirectly */
  71.  
  72.  
  73. void *
  74. MF3D_Malloc(
  75.     MF3DUns32    size)
  76. {
  77. #if defined(DEBUG) && DEBUG > 0
  78.     size_t *tempPtr;
  79.  
  80.     size += sizeof(*tempPtr);
  81.  
  82.     if (size > gMaxAllocSize)
  83.         return NULL;
  84.  
  85.     tempPtr = malloc((size_t)size);
  86.     if (tempPtr) {
  87.         *tempPtr++ = (size_t)size;
  88.         gMallocSize += size;
  89.         #if DEBUG > 1
  90.             printf("%d: malloc(%lx)\n", ++gMallocCount, tempPtr);
  91.         #endif
  92.     }
  93.     else {
  94.         printf("MF3D_Malloc: Error allocating %ld bytes."
  95.                 "%ld have already been allocated.\n",
  96.                 (long)size, (long)gMallocSize);
  97.     }
  98.  
  99.     return tempPtr;
  100. #else
  101. /* FB6_JRK
  102.  * Check to see that conversion from MF3DUns32 to size_t doesn't
  103.  * lose anything. Otherwise we might allocate something smaller.
  104.  */
  105.     if (size > gMaxAllocSize)
  106.         return NULL;
  107.  
  108. #if kUseNewPtrDirectly
  109.     return NewPtr((Size)size);
  110. #else
  111.     return malloc((size_t)size);
  112. #endif /* kUseNewPtrDirectly */
  113. #endif /* DEBUG */
  114. }
  115.  
  116.  
  117. void *
  118. MF3D_Realloc(
  119.     void *    ptr,
  120.     MF3DUns32    size)
  121. {
  122. #if defined(DEBUG) && DEBUG > 0
  123.     size_t *tempPtr;
  124.  
  125.     /* The C manual does not seem to explicitly state that
  126.      * realloc(NULL, size) is equivalent to malloc(size).
  127.      * So, to make sure we do not fail with some compiler somewhere,
  128.      * we will always malloc before realloc.
  129.      */
  130.     MFASSERT(size > 0);
  131.     
  132.     /* Handle the case where there is no existing pointer */
  133.     size += sizeof(*tempPtr);
  134.  
  135.     if (size > gMaxAllocSize)
  136.         return NULL;
  137.  
  138.     if (ptr == NULL)
  139.         tempPtr = malloc((size_t)size);
  140.     else
  141.         tempPtr = realloc((size_t *)ptr - 1, (size_t)size);
  142.  
  143.     if (tempPtr) {
  144.         if (ptr)
  145.             gMallocSize -= *tempPtr;
  146.         *tempPtr++ = (size_t)size;
  147.         gMallocSize += size;
  148.     }
  149.     else {
  150.         printf("MF3D_Realloc: Error allocating %ld bytes."
  151.                 "%ld have already been allocated.\n",
  152.                 (long)size, (long)gMallocSize);
  153.     }
  154.  
  155. /* FAK_JRK Now handled by above code.
  156.  *    MFASSERT(tempPtr != NULL);
  157.  */
  158.  
  159.     #if DEBUG > 1
  160.         if (tempPtr != ptr)
  161.         {    if (ptr == NULL)
  162.                 ++gMallocCount;
  163.             printf("%d: realloc(%lx to %lx)\n", gMallocCount, ptr, tempPtr);
  164.         }
  165.     #endif
  166.  
  167.     return tempPtr;
  168. #else
  169.     /* Handle the case where there is no existing pointer */
  170.     if (ptr == NULL)
  171.         return MF3D_Malloc(size);
  172.     /* FB6_JRK
  173.      * Check to see that conversion from MF3DUns32 to size_t doesn't
  174.      * lose anything. Otherwise we might allocate something smaller.
  175.      */
  176.     else if (size <= gMaxAllocSize)
  177. #if kUseNewPtrDirectly
  178.     {    void *tempPtr = NewPtr((Size)size);
  179.         if (tempPtr != NULL)
  180.         {    BlockMove(ptr, tempPtr, (Size)size);
  181.             DisposePtr(ptr);
  182.         }
  183.         return tempPtr;
  184.     }
  185. #else
  186.         return realloc(ptr, (size_t)size);
  187. #endif /* kUseNewPtrDirectly */
  188.     else
  189.         return NULL;
  190. #endif
  191. }
  192.  
  193.  
  194. void
  195. MF3D_Free(
  196.     void *    ptr)
  197. {
  198. #if defined(DEBUG) && DEBUG > 0
  199.     if (ptr) {
  200.         size_t *sizePtr = (size_t *)ptr - 1;
  201.         gMallocSize -= *sizePtr;
  202.         free(sizePtr);
  203.         #if DEBUG > 1
  204.             printf("%d: free(%lx)\n", --gMallocCount, ptr);
  205.         #endif
  206.     }
  207. #elif kUseNewPtrDirectly
  208.     DisposePtr((Ptr)ptr);
  209. #else
  210.     free(ptr);
  211. #endif
  212. }
  213.